package com.suxin.test.netty.adv.client;

import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import lombok.extern.slf4j.Slf4j;
import org.example.netty.adv.vo.Message;
import org.example.netty.adv.vo.MessageHeader;
import org.example.netty.adv.vo.MessageType;

/**
 * @Author: tang
 * @Description: client 业务处理 handler
 * @Date 2025/1/21 09:47
 */
@Slf4j
public class ClientBusinessInBoundChannelHandler extends SimpleChannelInboundHandler<Message> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, Message msg) throws Exception {
        MessageHeader messageHeader = msg.getMessageHeader();
        byte type = messageHeader.getType();
        // 需要业务应答
        if (type == MessageType.SERVICE_RESP.value()) {
            log.info("client handler service_resp msg...");
            CilentBusiRespQueue.putResult(msg);
        }else {
            System.out.println("client accept server msg: " + msg);
        }
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        log.info("client channelActive...");

        ctx.writeAndFlush(Message.buildSendServerMsg("hello server!"));
    }
}
